package com.samsung.spen.sshare.explorer.choosefiles;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.samsung.spen.sshare.R;
import com.samsung.spen.sshare.explorer.util.FileExtend;
import com.samsung.spen.sshare.explorer.util.FileSelected;
import com.samsung.spen.sshare.explorer.util.FilesSelectedAdapter;
import com.samsung.spen.sshare.explorer.util.SConfig;

public class FilesSelectedActivity extends ListActivity {

	static ArrayList<FileSelected> data;
	FilesSelectedAdapter adapter;

	Handler mHandler;

	ArrayList<String> arrayListFilePaths = new ArrayList<String>();
	ArrayList<String> listFilesFinal = new ArrayList<String>();

	String ip;
	String port;
	
	public static TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exp_files_selected_list);

		textView = (TextView)findViewById(R.id.tvNum);
		mHandler = new Handler();

		Intent intent = getIntent();
		arrayListFilePaths = intent
				.getStringArrayListExtra(SConfig.PATH_LIST_FILES);

		if (arrayListFilePaths != null && arrayListFilePaths.size() > 0) {

			data = getFileArray(arrayListFilePaths);
			adapter = new FilesSelectedAdapter(this, mHandler,
					R.layout.exp_files_selected_item, data);
			setListAdapter(adapter);
		}

	}

	public ArrayList<FileSelected> getFileArray(ArrayList<String> listPaths)
			throws NullPointerException {

		ArrayList<FileSelected> listFile = new ArrayList<FileSelected>();

		if (listPaths.size() > 0) {

			for (String file : listPaths) {

				if (file != null) {

					FileSelected fExtend = new FileSelected(file);
					listFile.add(fExtend);
				}

			}
		}

		return listFile;
	}

	public void refresh(ArrayList<FileSelected> files) {

		if (files == null) {

			setListAdapter(null);
			return;
		}

		adapter.notifyDataSetChanged();

		setListAdapter(adapter);

	}

	@Override
	protected void onStart() {
		super.onStart();
		refresh();
	}

	public void refresh() {

		// // data = getFileArray();
		// refresh(data);

		new LoadFiles().execute();
	}

	public void onclick(View v) {

		switch (v.getId()) {

		case R.id.btnAddFiles:

			listFilesFinal.clear();
			Intent intent = new Intent(this, FilesChooseActivity.class);

			if (data != null && data.size() > 0) {

				for (FileSelected iterable_element : data) {

					if (iterable_element.isSelect()) {

						listFilesFinal.add(iterable_element.getFile()
								.getAbsolutePath());
					}
				}
			}

			intent.putStringArrayListExtra(SConfig.PATH_LIST_FILES,
					listFilesFinal);
			startActivityForResult(intent, SConfig.REQUEST_ADD_FILE);
			// this.finish();

			break;

		case R.id.btnOK:

			listFilesFinal.clear();
			// Intent intent2 = new Intent(this, MainActivity.class);

			for (FileSelected iterable_element : data) {

				if (iterable_element.isSelect()) {

					listFilesFinal.add(iterable_element.getFile()
							.getAbsolutePath());
				}
			}

			if (listFilesFinal.size() > 0) {
				// process array list files
				StringBuilder dataToSend = new StringBuilder();

				for (String item : listFilesFinal) {

					FileExtend itemExtend = new FileExtend(item);
					dataToSend.append(itemExtend.toString() + ";");
				}
				// remove last ';'
				dataToSend.substring(0, dataToSend.length() - 1);

				// Intent iSelectedFiles = new
				// Intent(SConfig.BROASTCAST_EXPLORER);
				// iSelectedFiles.putExtra(SConfig.BROASTCAST_DATA,
				// dataToSend.toString());
				// // intent2.putStringArrayListExtra(SConfig.PATH_LIST_FILES,
				// listFilesFinal);
				// sendBroadcast(iSelectedFiles);

				// start Activity to Hai
				Intent result = getIntent();
				//result.setAction(SConfig.EXTRA_ACTION);
				result.putExtra(SConfig.EXTRA_DATA, dataToSend.toString());
				
				Log.e("Data to send==>",dataToSend.toString());
				data = null;
				
				setResult(RESULT_OK, result);
				
//				result.putStringArrayListExtra(SConfig.PATH_LIST_FILES,
//						listFilesFinal);
//				this.sendBroadcast(result);

				// Intent sendFile=new Intent(this, SendReceiveActivity.class);
				// sendFile.putExtra(SConfig.EXTRA_DATA_IP, ip);
				// sendFile.putExtra(SConfig.EXTRA_DATA_PORT, port);
				// startActivity(sendFile);
				this.finish();
			}

			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent dataIntent) {
		super.onActivityResult(requestCode, resultCode, dataIntent);

		if (resultCode == RESULT_OK) {

			if (requestCode == SConfig.REQUEST_ADD_FILE) {

				arrayListFilePaths = dataIntent
						.getStringArrayListExtra(SConfig.PATH_LIST_FILES);

				if (arrayListFilePaths.size() > 0) {

					data = getFileArray(arrayListFilePaths);
					adapter = new FilesSelectedAdapter(this, mHandler,
							R.layout.exp_files_selected_item, data);
					setListAdapter(adapter);
					
					textView.setText("Num: " + adapter.getCount());
					//textView.setVisibility(View.GONE);
				}
			}
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		if (arrayListFilePaths != null)
			arrayListFilePaths.clear();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.exp_select_all, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		SConfig.FLAG_SELECT_FILE_TO_SEND_ALL = !SConfig.FLAG_SELECT_FILE_TO_SEND_ALL;
		refresh();
		return super.onOptionsItemSelected(item);
	}

	private class LoadFiles extends AsyncTask<Void, Integer, Void> {

		private ProgressDialog dialog;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = ProgressDialog.show(FilesSelectedActivity.this, "",
					"loading...");
		}

		@Override
		protected Void doInBackground(Void... params) {

			if (arrayListFilePaths != null && arrayListFilePaths.size() > 0) {

				data = getFileArray(arrayListFilePaths);
			} else {
				return null;
			}

			if (data == null) {

				// showToast("Folder is Empty!");
				setListAdapter(null);
				return null;
			}

			adapter = new FilesSelectedAdapter(FilesSelectedActivity.this,
					mHandler, R.layout.exp_files_selected_item, data);

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);

			setListAdapter(adapter);

			dialog.dismiss();
			dialog = null;
		}
	}
}
